/* EasyCODE V8 */
/* EasyCODE ( 0 */
#ifndef _INIT_H
    #define _INIT_H

    #include <p33FJ16GS504.h>

    /* EasyCODE ( 0 
    PIN Definition */
    #define GLED LATCbits.LATC4
    #define WLED LATCbits.LATC5
    #define BLED LATCbits.LATC6

#define ADCISR_ENTER GLED = 1
#define ADCISR_EXIT GLED = 0
#define T3ISR_ENTER WLED = 1
#define T3ISR_EXIT WLED = 0
#define T1CALC_ENTER BLED = 1
#define T1CALC_EXIT BLED = 0

    #define MAA LATAbits.LATA1
    #define MBA LATCbits.LATC13
    #define MCA LATCbits.LATC0

    #define MA_TRIS(x) { TRISAbits.TRISA1 = x; TRISCbits.TRISC13 = x; TRISCbits.TRISC0 = x; }
    #define A1 PORTCbits.RC10
    #define A2 PORTCbits.RC9
    #define A3 PORTBbits.RB10


    #define WORD_FIRMWARE_VERSION           0x00000800
    #define WORD_PIC_DEVICE                 0x00FF0000

    /* EasyCODE ) */
    /* EasyCODE ( 0 
    Constants */
    #define FCY 40000000UL //Instruction cycle rate (Osc x PLL / 4)

//    #define PWM_FREQUENCY_VALUE 23675 // = 40khz

//    #define PWM_BEMF_TRESHOLD 10000
//    #define PWM_BEMF_TRESHOLD_LOW 6000

//    #define PWM_SAMPLE_BEMF_OFF 18859

//    #define PWM_SAMPLE_BEMF_ON_LIMIT 17682
//    #define PWM_SAMPLE_BEMF_ON 100

    //#define BEMF_BUS_LOW 3

//    #define PWM_DUTYCYCLE_MIN 500    // = 2.1%
//    #define PWM_DUTYCYCLE_MAX 23000  // = 97.5%

    /* EasyCODE [ 
    Openloop */
//    #define OPENLOOP_COMMUTATIONS   (12)
//    #define OPENLOOP_POWER_START    3500
//    #define OPENLOOP_POWER_END      1500
//    #define OPENLOOP_SPEED_START    60000
//    #define OPENLOOP_SPEED_END       55000
//    #define OPENLOOP_AFTER_ZC_COUNT 12

    /* EasyCODE ] */
    /* EasyCODE ) */
    /* EasyCODE ( 0 
    Function definitions */

struct setup_t
{
    unsigned int CONTROL_LOOP; // in hz

    unsigned int TIMEOUT;

    unsigned int PID_KP; //= 102; // ==> 0.1 * 1024
    unsigned int PID_KI; // = 131; // ==> 1 * dt * 65536 * 234.67
    unsigned int MOTOR_POLES;
    unsigned int MOTOR_CURRENT_LIMIT;
    unsigned int MOTOR_MIN_RPM;
    unsigned int MOTOR_MAX_RPM;

    unsigned int TIMING_DEGREES;

    unsigned int errortreshold;

    unsigned int OPENLOOP_POWER_START;
    unsigned int OPENLOOP_SPEED_START;
    unsigned int OPENLOOP_RPM_START;
    unsigned int OPENLOOP_ZC;

    unsigned int FEEDBACK_VAR_1;
    unsigned int FEEDBACK_VAR_2;
    unsigned int FEEDBACK_VAR_3;

    unsigned int PID_LIMIT_ERROR;

    unsigned int PWM_FREQ;

    unsigned int BEMF_ON_PROM;
    unsigned int BEMF_OFF_PROM;
    unsigned int SMP_BEMF_OFF;
    unsigned int SMP_BEMF_ON;
    unsigned int SMP_BEMF_ON_LIM;
    unsigned int PWM_MIN;
    unsigned int PWM_MAX;
    unsigned int PWM_DEADTIME;
    unsigned int BUS_LOW;
    
    unsigned int calcPWM_FREQ;
    unsigned int calcBEMF_ON_PROM;
    unsigned int calcBEMF_OFF_PROM;
    unsigned int calcSMP_BEMF_OFF;
    unsigned int calcSMP_BEMF_ON;
    unsigned int calcSMP_BEMF_ON_LIM;
    unsigned int calcPWM_MIN;
    unsigned int calcPWM_MAX;
};

struct setupmeta_t
{
    unsigned char ID;
    const char * name;
    unsigned int * pointer;
    unsigned int def;
};


struct ADC_t
{
    unsigned int MA;
    unsigned int MB;
    unsigned int MC;
    unsigned int CURRENT;
    unsigned int BUS;

    struct {
        unsigned char MA, MB, MC;
        unsigned char step;
    } debug[256];

    unsigned char debugPos;
};



struct state_t {
    volatile enum {
        STATE_CONFIG = 0,
        STATE_OFF,
        STATE_ALIGN,
        //STATE_OPENLOOP,
        //STATE_OPENLOOP_BEMF,
        STATE_BEMF_COMMUTATION,
        STATE_BEMF_SAMPLING,
        STATE_STOPPING
    } mode;

    unsigned char step;
    volatile unsigned char DEMAG;

    unsigned bodeMode:1;
    unsigned BEMFmode:1;
    unsigned zc_found:1;
    unsigned direction:1;
    volatile unsigned armed:1;
    
    unsigned int rampup;

    unsigned int RPM;
    unsigned int errorcount;
    unsigned int timeoutCount;
    unsigned int bodeTimeout;
    
    unsigned int zerocrossAvgArray[6];
    //volatile unsigned int zerocrossAvg;

    //unsigned int DEMAGtime;
    volatile unsigned int BEMFSTARTCOUNT;
    unsigned int delay_num;
    unsigned int delay_denom;
    
    volatile unsigned int RPMpulse;
    volatile unsigned int RPMpulseDiv;

    unsigned int debugDemag[6];
    unsigned int debugZC[6];
};

struct pid_t
{
    unsigned int output;
    int p_value;
    int i_value;

    //    long k_p;
    //    long k_i;

    int target_error;

    volatile int target_rpm;
};

struct bode_t
{
    // Set RPM...
    // Enter bode mode
    // <wait x seconds>

    // I2C sets first signal output [count=1] - NO bode feedback
    // Main loop put signal on MDC
    // Put error & count on I2C queue [count=1]
    // I2C sets next signal output - empty queue [
    // Main loop put signal on MDC

    unsigned hasFeedback:1;

    int feedbackError;
    int feedbackPID;
    int feedbackSignal;

    unsigned hasSignalToInject:1;
    unsigned missedSignal:1;
    int signalToInject;
    
    // actual injected signal
    int signalInjected;

};



/** MUSICA! */

#define KEY_A4 0
#define KEY_AF4 1
#define KEY_B4 2
#define KEY_C4 3
#define KEY_CF4 4
#define KEY_D4 5
#define KEY_DF4 6
#define KEY_E4 7
#define KEY_F4 8
#define KEY_FF4 9
#define KEY_G4 10
#define KEY_GF4 11
#define KEY_A5 12
#define KEY_AF5 13
#define KEY_B5 14
#define KEY_C5 15
#define KEY_CF5 16
#define KEY_D5 17
#define KEY_DF5 18
#define KEY_E5 19
#define KEY_F5 20
#define KEY_FF5 21
#define KEY_G5 22
#define KEY_GF5 23
#define KEY_A6 24
#define KEY_AF6 25
#define KEY_B6 26
#define KEY_C6 27
#define KEY_CF6 28
#define KEY_D6 29
#define KEY_DF6 30
#define KEY_E6 431
#define KEY_F6 32
#define KEY_FF6 33
#define KEY_G6 34
#define KEY_GF6 35
#define KEY_A7 36
#define KEY_AF7 37
#define KEY_B7 38
#define KEY_C7 39
#define KEY_CF7 40
#define KEY_D7 41
#define KEY_DF7 42
#define KEY_E7 43
#define KEY_F7 44
#define KEY_FF7 45
#define KEY_G7 46
#define KEY_GF7 47

#define NOTE_16 0x00
#define NOTE_8 0x40
#define NOTE_4 0x80
#define NOTE_2 0xC0





/* EasyCODE ) */
#endif 
/* EasyCODE ) */
